/*  static char SccsId[] = "@(#)farlfunc.h	1.22   2/16/96";
 *
 *         Copyright (c) KUBOTA Corporation and HUDSON SOFT,
 *                  1994, 1995 All Rights Reserved
 *
 *  This software comprises unpublished confidential information of
 *  KUBOTA Corp. and HUDSON SOFT. It may not be used, copied or made
 *  available to anyone, except in accordance with the license under
 *  which it is furnished.
 */

#ifndef _FARLFUNC_H_
#define _FARLFUNC_H_

/************************************************************************
 *   Function Defintions 
 ************************************************************************/
/*
 * Base functions
 */
int  FarlInit(void);
void FarlWriteFifoH(unsigned short data);
void FarlWriteFifoW(unsigned int data);
void FarlWriteFifoHA(unsigned short *data, int num);
void FarlWriteFifoWA(unsigned int *data, int num);
void FarlSetPEReg(int id, unsigned short data);
void FarlSetTEReg(int id, unsigned short data);
void FarlReset(int sw);
unsigned short FarlGetTEReg(int regid, int sw);
unsigned short FarlGetPEReg(int regid, int sw);
void FarlWriteTxt(unsigned short offset, unsigned short data);
void FarlWriteTxtUnpk(unsigned short offset, unsigned short data);
void FarlWriteTxtXY(unsigned short x, unsigned short y, unsigned short data);
void FarlWriteTxtXYUnpk(unsigned short x, unsigned short y,
	unsigned short data);
void FarlSetLut(int id, unsigned short d);
unsigned short FarlGetLut(int id, int sw);

/************************************************************************
 *   Parameter Defintions 
 ************************************************************************/
#define FARL_SET_LIGHT1		(1<<0)	/* Set light vector 1*/
#define FARL_SET_LIGHT2		(1<<1)	/* Set light vector 2*/
void FarlSetLight(int num, unsigned short x, unsigned short y,
	unsigned short z);

/*
 * Primitive functions
 */
/************************************************************************
 *   Type Defintions 
 ************************************************************************/
#define FARL_GTYPE_VC		(0x0<<8) /* Vertex color, w.o. normal   */
#define FARL_GTYPE_VCVN		(0x1<<8) /* Vertex color, Vertex normal */
#define FARL_GTYPE_FCFN		(0x2<<8) /* Facet color, Facet normal   */
#define FARL_GTYPE_TX		(0x4<<8) /* Texture map, w.o. normal    */
#define FARL_GTYPE_TXVN		(0x5<<8) /* Texture map, Vertex normal  */
#define FARL_GTYPE_TXFN		(0x6<<8) /* Texture map, Facet normal   */
#define FARL_GTYPE_DC		(0x8<<8) /* Default color, w.o. normal  */
#define FARL_GTYPE_DCVN		(0x9<<8) /* Default color, Vertex normal*/
#define FARL_GTYPE_DCFN		(0xa<<8) /* Default color, Facet normal */

void FarlPutImage(unsigned short *img, int xleft, int ytop, int xright,
	int ybottom, unsigned short z );
void FarlPutDZImage(unsigned short *img, int xleft, int ytop, int xright,
	int ybottom );
void FarlPutTextureImage(unsigned short *img, int xleft, int ytop, int xright,
	int ybottom );
void FarlFillBuffer(unsigned short xleft, unsigned short ytop,
	unsigned short xright, unsigned short ybottom, unsigned short d,
	unsigned short z );
int FarlTriangleStrip(int type, int nelem, unsigned short *vert,
	unsigned short *norm, unsigned short *color, unsigned short *vtexi);
int FarlTriangleList(int type, int nelem, unsigned short *vert,
	unsigned short *norm, unsigned short *color, unsigned short *vtexi);
int FarlPolyLine(int type, int nelem, unsigned short *vert,
	unsigned short *norm, unsigned short *color);
int FarlLineList(int type, int nelem, unsigned short *vert,
	unsigned short *norm, unsigned short *color);
int FarlPointList(int type, int nelem, unsigned short *vert,
	unsigned short *norm, unsigned short *color);

/*
 * Command functions
 */
unsigned short FarlReadPixelDZ(unsigned short x, unsigned short y, int sw);
unsigned short FarlReadPixelTxt(unsigned short x, unsigned short y, int sw);

/*
 * TE register functions
 */
void FarlSetLightCoef(unsigned short amb, unsigned short dif1,
	unsigned short dif2, unsigned short sp1, unsigned short sp2);
void FarlSetLight1(unsigned short x, unsigned short y, unsigned short z);
void FarlSetLight2(unsigned short x, unsigned short y, unsigned short z);
void FarlSetTEMeshPatternReg(unsigned short d);
void FarlSetSrcMatrix(unsigned short *m);
void FarlSetSrcMatrix3x3(unsigned short *m);
void FarlSetSrcMatrix3x1(unsigned short *m);
void FarlSetSrcMatrix6(unsigned short *m);
void FarlSetTEControlReg(unsigned short d);
void FarlSetWindowClipReg(unsigned short xl, unsigned short yt,
	unsigned short xr, unsigned short yb);
void FarlSetWindowScaleReg(unsigned short sx, unsigned short tx,
	unsigned short sy, unsigned short ty);
void FarlSetTEObjMatrix( unsigned short *m );
void FarlSetTENrmMatrix( unsigned short *m );
void FarlSetUVOffsetReg(unsigned short u, unsigned short v);
void FarlSetDefColorReg(unsigned short c);

/*
 * PE register functions 
 */
void FarlSetPEClearTextureReg(unsigned short d);
void FarlSetPEClearZReg(unsigned short d);
void FarlSetPEClearColorReg(unsigned short d);
void FarlSetPEClearMaskReg(unsigned short d);
void FarlSetPEFlashICReg(unsigned short d);
void FarlSetPEFrameCtrlReg(unsigned short d);
void FarlSetPEICMaskReg(unsigned short d);
void FarlSetLut1(unsigned short d);
void FarlSetLut2(unsigned short d);
void FarlSetLut3(unsigned short d);
void FarlSetPEControlReg(unsigned short d);
void FarlSetPEMaskReg(unsigned short d);
void FarlSetPETargetReg(unsigned short d);
void FarlSetPETextureReg(unsigned short d);
void FarlSetPECwtXOffReg(unsigned short d);
void FarlSetPECwtYOffReg(unsigned short d);

/*
 * Matrix operation functions
 */
void FarlCopyDstToNrm(void);
void FarlCopyDstToObj(void);
void FarlCopyDstToRst(void);
void FarlCopyDstToSrc(void);
void FarlCopySrcToRst(void);
void FarlCopyRstToSrc(void);
void FarlCopyDstToLgt1(void);
void FarlCopyDstToLgt2(void);
void FarlMul3x3M1015(unsigned short *m);
void FarlMul4x4M1015(unsigned short *m);
void FarlMul3x3M187(unsigned short *m);
void FarlMul4x4M187(unsigned short *m);
void FarlMul3x1V1015(unsigned short x, unsigned short y, unsigned short z);
void FarlTESync(int sw);
void FarlVertexTest(int num, unsigned short *v);

/*
 * Sprite support functions.
 */
void FarlSetSpControlTbl(unsigned short no, unsigned short d[16] );
void FarlSetSpParamTbl(unsigned short addr, unsigned short num,
	unsigned short *d );
void FarlSetSpWinClipReg(unsigned short xl, unsigned short yt,
	unsigned short xr, unsigned short yb );
void FarlSpriteGo(unsigned short bnk, unsigned short no, unsigned short wc );
void FarlSpbsySpin(void);
void FarlWaitSpdone(void);
void FarlClearSpdone(void);

/*
 * CMT support functions.
 */
void FarlSetCmt(unsigned short addr, unsigned short sz, unsigned short *d );
void FarlCmtGo(unsigned short bnk, unsigned short addr, unsigned short bc );
void FarlCmbsySpin(void);
void FarlCmpndSpin(void);
void FarlWaitCmdone(void);
void FarlClearCmdone(void);

/*
 * Utility functions
 */
unsigned short FarlFixMul1015(unsigned short a, unsigned short b );
unsigned short FarlFixMul187(unsigned short a, unsigned short b );
unsigned char FarlInportB(unsigned int addr);
unsigned short FarlInportH(unsigned int addr);
unsigned int FarlInportW(unsigned int addr);
void FarlOutportB(unsigned int addr, unsigned char val);
void FarlOutportH(unsigned int addr, unsigned short data );
void FarlOutportW(unsigned int addr, unsigned int data );
void FarlPerseZ4x4M187(unsigned short m);
void FarlRotateX3x3M1015(int angle);
void FarlRotateX4x4M187(int angle);
void FarlRotateY3x3M1015(int angle);
void FarlRotateY4x4M187(int angle);
void FarlRotateZ3x3M1015(int angle);
void FarlRotateZ4x4M187(int angle);
void FarlRotateXSC3x3M1015(unsigned short sin1015, unsigned short cos1015);
void FarlRotateYSC3x3M1015(unsigned short sin1015, unsigned short cos1015);
void FarlRotateZSC3x3M1015(unsigned short sin1015, unsigned short cos1015);
void FarlRotateXSC4x4M187(unsigned short sin187, unsigned short cos187);
void FarlRotateYSC4x4M187(unsigned short sin187, unsigned short cos187);
void FarlRotateZSC4x4M187(unsigned short sin187, unsigned short cos187);
void FarlTranslate4x4M187(unsigned short x, unsigned short y,unsigned short z);
void FarlScale4x4M187(unsigned short sx, unsigned short sy, unsigned short sz);
void FarlSwapWindow(void);
void FarlSwapWindowH(void);
void FarlValidateSwap(void);
void FarlDummySwap(void);
void FarlSetUnitMatrix1015(void);
void FarlSetUnitMatrix187(void);
unsigned short FarlFix1015( float data );
unsigned short FarlFix187( float data );
float FarlFloat1015( unsigned short data );
float FarlFloat187( unsigned short data );
void FarlNormalizeVector( unsigned short *v );
unsigned short FarlGetSin187( int angle );
unsigned short FarlGetSin1015( int angle );
unsigned short FarlGetCos187( int angle );
unsigned short FarlGetCos1015( int angle );
void FarlCopyDstToMem4x4( unsigned short *m );
void FarlCopyDstToMem3x3( unsigned short *m );
void FarlCopyDstToMem3x1( unsigned short *v );
void FarlGetRotateX3x3M1015( int angle, unsigned short *m );
void FarlGetRotateY3x3M1015( int angle, unsigned short *m );
void FarlGetRotateZ3x3M1015( int angle, unsigned short *m );
void FarlGetRotateXSC3x3M1015( unsigned short sin, unsigned short cos,
	unsigned short *m );
void FarlGetRotateYSC3x3M1015( unsigned short sin, unsigned short cos,
	unsigned short *m );
void FarlGetRotateZSC3x3M1015( unsigned short sin, unsigned short cos,
	unsigned short *m );
void FarlGetRotateX4x4M187( int angle, unsigned short *m );
void FarlGetRotateY4x4M187( int angle, unsigned short *m );
void FarlGetRotateZ4x4M187( int angle, unsigned short *m );
void FarlGetRotateXSC4x4M187( unsigned short sin, unsigned short cos,
	unsigned short *m );
void FarlGetRotateYSC4x4M187( unsigned short sin, unsigned short cos,
	unsigned short *m );
void FarlGetRotateZSC4x4M187( unsigned short sin, unsigned short cos,
	unsigned short *m );
void FarlGetTranslate4x4M187( unsigned short x, unsigned short y,
	unsigned short z, unsigned short *m );
void FarlGetScale4x4M187( unsigned short sx, unsigned short sy,
	unsigned short sz, unsigned short *m );
void FarlMatrixCopy3x3( unsigned short *dst, unsigned short *src );
void FarlMatrixCopy4x4( unsigned short *dst, unsigned short *src );

/* 
 * Get Command Packet Size Functions.
 */
int FarlGetTriangleStripSize( int type, int nelem );
int FarlGetTriangleListSize( int type, int nelem );
int FarlGetPolyLineSize( int type, int nelem );
int FarlGetLineListSize( int type, int nelem );
int FarlGetPointListSize( int type, int nelem );
/*
 * Dump Vector Functions
 */
int FarlDumpTriangleStrip(int type, int nelem, unsigned short *vert,
	unsigned short *norm, unsigned short *color, unsigned short *vtexi,
	unsigned short *dbuf );
int FarlDumpTriangleList(int type, int nelem, unsigned short *vert,
	unsigned short *norm, unsigned short *color, unsigned short *vtexi,
	unsigned short *dbuf );
int FarlDumpPolyLine(int type, int nelem, unsigned short *vert,
	unsigned short *norm, unsigned short *color, unsigned short *dbuf );
int FarlDumpLineList(int type, int nelem, unsigned short *vert,
	unsigned short *norm, unsigned short *color, unsigned short *dbuf );
int FarlDumpPointList(int type, int nelem, unsigned short *vert,
	unsigned short *norm, unsigned short *color, unsigned short *dbuf );

/************************************************************************
 *   Type definitions
 ************************************************************************/
typedef struct {
     unsigned char tbwidth;
     unsigned char tsize;
     unsigned char rev;
} farl_conf_t;

int FarlGetConf( farl_conf_t *conf );

/************************************************************************
 *   Function Macro Defintions 
 ************************************************************************/
#ifndef _LIBFARL_H_
#define FarlInportB(addr)       	__INPORTB(addr)
#define FarlInportH(addr)       	__INPORTH(addr)
#define FarlInportW(addr)       	__INPORTW(addr)
#define FarlOutportB(addr,data) 	__OUTPORTB(addr,data)
#define FarlOutportH(addr,data) 	__OUTPORTH(addr,data)
#define FarlOutportW(addr,data) 	__OUTPORTW(addr,data)
#define FarlSetMemReg(addr, data)	__OUTPORTH(addr,data)
#define FarlGetMemReg(addr)		__INPORTH(addr)
#endif	/* _LIBFARL_H_ */

#endif	/* _FARLFUNC_H_ */
